package com.ebelter.temperature.activitys;


import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.activity.BaseActivity;

import com.ebelter.btcomlib.models.bluetooth.base.BlueManager;
import com.ebelter.btcomlib.models.bluetooth.base.BlueScan;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IBlueStationListener;
import com.ebelter.btcomlib.models.bluetooth.interfaces.IScanCallback;
import com.ebelter.btcomlib.models.bluetooth.products.temperature.TemperatureConstant;
import com.ebelter.btcomlib.models.bluetooth.products.temperature.TemperatureManager;
import com.ebelter.btcomlib.utils.StatusBarUtil;

import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.ViewUtils;

import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.temperature.R;
import com.ebelter.temperature.adapters.TBandDeviceAdapter;

import java.util.List;


public class T_BandDeviceActivity extends BaseActivity {

    private static final String TAG="T_BandDeviceActivity";
    public static final int REQUEST_CODE = 104;


    LinearLayout no_open_blue_tip_ll;
    LinearLayout search_blue_tip_ll;

    TextView topTitleTv;

    private BlueScan mBlueScan;

    private ListView mListView;
    private TBandDeviceAdapter mTBandDeviceAdapter;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_t_banddevice;
    }

    @Override
    protected void initView() {
        FV();
    }

    private void FV() {
        StatusBarUtil.setColor(this, Color.parseColor("#787ede"));

        no_open_blue_tip_ll = findViewById(R.id.no_open_blue_tip_ll);
        search_blue_tip_ll = findViewById(R.id.search_blue_tip_ll);

        topTitleTv = findViewById(R.id.top_title_tv);
        mListView=findViewById(R.id.band_lv);

    }

    private IBlueStationListener mIBlueStationListener;

    @Override
    protected void initData() {
        topTitleTv.setText("绑定设备");
        mBlueScan=new BlueScan(this);
        mBlueScan.setScanCallback(new IScanCallback() {
            @Override
            public void beforeScan() {

            }

            @Override
            public void scanOneDevice(BluetoothDevice device, int rssi, byte[] scanRecord) {
                LogUtils.i(TAG,"扫描到设备：name="+device.getName()+"---address="+device.getAddress());
                if (TextUtils.isEmpty(device.getName())){
                    return;
                }
                if (!TextUtils.equals(device.getName(), TemperatureConstant.T_NAME)){
                    return;
                }
                if (mTBandDeviceAdapter!=null){
                    mTBandDeviceAdapter.addOne(device);

                    if (mTBandDeviceAdapter.getCount()>0){
                        ViewUtils.hideView(search_blue_tip_ll);
                    }else {
                        ViewUtils.displayView(search_blue_tip_ll);
                    }

                }





            }

            @Override
            public void overScan() {
                if (mBlueScan!=null){
                    mBlueScan.startScanBluetoothDevices();
                }

            }
        });

        mIBlueStationListener=new IBlueStationListener() {
            @Override
            public void STATE_OFF() {checkBlueStation();}

            @Override
            public void STATE_TURNING_OFF() {checkBlueStation();}

            @Override
            public void STATE_ON() {checkBlueStation();}

            @Override
            public void STATE_TURNING_ON() {checkBlueStation();}
        };

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<BluetoothDevice> devices = mTBandDeviceAdapter.getmDatas();
                BluetoothDevice device=devices==null?null:devices.get(position);

                if (device==null||TextUtils.isEmpty(device.getAddress())){
                    return;
                }

                mTBandDeviceAdapter.bandAddress=device.getAddress();
                mTBandDeviceAdapter.bandName=device.getName();
                mTBandDeviceAdapter.notifyDataSetChanged();


            }
        });



        BlueManager.getInstance().addBluetoothStationListener(mIBlueStationListener);
        mTBandDeviceAdapter=new TBandDeviceAdapter(this);
        mListView.setAdapter(mTBandDeviceAdapter);


    }

    //开始扫描设备
    private void startScan(){
        LogUtils.i(TAG,"开始扫描设备--startScan（）");
        if (mBlueScan!=null) mBlueScan.startScanBluetoothDevices();
    }

    @Override
    protected void onResume() {
        super.onResume();
        checkBlueStation();
        startScan();
    }

    private void  checkBlueStation(){
        if (BlueManager.getInstance().isBluetoothOpen()){
            //蓝牙开关打开
            ViewUtils.goneView(no_open_blue_tip_ll);
            startScan();
        }else {
            //蓝牙开关关闭
            ViewUtils.displayView(no_open_blue_tip_ll);
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        BlueManager.getInstance().removeBluetoothStationListener(mIBlueStationListener);
        if (mBlueScan!=null){
            mBlueScan.stopScanBluetoothDevices();
            mBlueScan=null;}
    }

    //返回键被按下
    public void backClick(View view){
        LogUtils.i(TAG,"返回键被按下");
        finish();
    }




    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);



    }


    //绑定设备按钮
    public void bandDevicesClick(View view){

        if (mTBandDeviceAdapter==null){
            return;
        }

        String address=mTBandDeviceAdapter.bandAddress;
        if (TextUtils.isEmpty(address)){
            ToastUtil.show("请先选择蓝牙设备");
            return;
        }

        TemperatureManager.setBondDeviceAddress(address);
        TemperatureManager.setBondDeviceName(address);
        setResult(1);
        finish();



    }
}
